package com.poobo.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.poobo.base.impl.BaseDaoImpl;
import com.poobo.condiction.EquipmentConfigListCondiction;
import com.poobo.dao.IEquipmentConfiglistDao;
import com.poobo.entity.TbEquipmentConfiglist;
import com.poobo.util.pager.Page;

@Repository
public class EquipmentConfiglistDaoImpl extends BaseDaoImpl implements IEquipmentConfiglistDao{

	@Override
	public TbEquipmentConfiglist queryEquipmentConfigById(Integer id) {
		return selectObjectById(TbEquipmentConfiglist.class, id);
	}

	@Override
	public Page queryEquipmentConfigPage2(
			TbEquipmentConfiglist equipmentConfiglist, Page page) {
		String hql = " from TbEquipmentConfiglist where 1=1 ";
		String countHql = " select count(*) from TbEquipmentConfiglist where 1=1 ";
		List<Object> params = new ArrayList<Object>();
		String orderBy = " order by id desc";
		StringBuffer where = new StringBuffer();
		if( equipmentConfiglist != null ){

			if( equipmentConfiglist.getEid() != null ){
				where.append(" and eId = ? ");
				params.add(equipmentConfiglist.getEid());
			}
			if( equipmentConfiglist.getConfigListId() != null ){
				where.append(" and configListId = ? ");
				params.add(equipmentConfiglist.getConfigListId());
			}
		}
		page.setBaseHql(hql + where.toString() + orderBy);
		page.setCountHql(countHql + where.toString());
		page.setParam(params);
		return getPageByHql(page);
	}

	@Override
	public void deleteEquipmentConfigById(Integer id) {
		String hql = " delete from TbEquipmentConfiglist where id="+ id +" ";
		delObjectByIds(hql);
	}

	@Override
	public void saveEquipmentConfig(TbEquipmentConfiglist equipmentConfiglist) {
		saveOrUpdate(equipmentConfiglist);
	}

	@Override
	public Page queryEquipmentConfigPage(EquipmentConfigListCondiction condiction,Page page) {
		String baseSql = " select eqc.ecid as id,eqc.eId,eqc.configListId,eqc.number,eqc.cname " +
				"from " +
				"(select ec.ID as ecid,eq.ID as eqid, cl.ID as clid,ec.configListId as configListId,ec.eId as eId,eq.number as number,cl.`name` as cname " +
				"from tb_equipment_configlist ec " +
				"left join tb_equipment eq on ec.eId=eq.ID " +
				"left join tb_config_list cl on ec.configListId=cl.ID " +
				"where 1=1  ";
		String countSql = " select count(*) " +
				"from " +
				"(select ec.ID as ecid,eq.ID as eqid, cl.ID as clid,ec.configListId as configListId,ec.eId as eId,eq.number as number,cl.`name` as cname " +
				"from tb_equipment_configlist ec " +
				"left join tb_equipment eq on ec.eId=eq.ID " +
				"left join tb_config_list cl on ec.configListId=cl.ID " +
				"where 1=1  ";
		StringBuffer where = new StringBuffer();
		List<Object> params = new ArrayList<Object>();
		String groupBy = " ) as eqc group by eqc.ecid ";
		String orderBy = "  order by eqc.ecid desc ";
		
		if ( condiction.getEquipment()  !=null && condiction.getEquipment().getNumber() != null && condiction.getEquipment().getNumber() != "") {
			where.append(" and eq.number like '%" + condiction.getEquipment().getNumber() + "%' ");
		}
		page.setBaseSql(baseSql + where.toString() + groupBy + orderBy);
		page.setCountSql(countSql + where.toString() + groupBy);
		page.setParam(params);
		Page page2 = getPageBySql(page);
		page2.setPageList(getMapBySql(baseSql + where.toString() + groupBy + orderBy, null));
		return page2;
	}

	@Override
	public void deleteEquipmentConfigByEquipmentId(Integer eId) {
		String sql = " delete from tb_equipment_configlist where eId="+ eId +" ";
		execSql(sql);
	}

	@Override
	public void deleteEquipmentConfigByConfigListId(Integer configListId) {
		String sql = " delete from tb_equipment_configlist where configListId="+ configListId +" ";
		execSql(sql);
	}

	@Override
	public List<TbEquipmentConfiglist> findAll() {
		String hql = " from TbEquipmentConfiglist ";
		return (List<TbEquipmentConfiglist>)findAll(hql);
	}

	@Override
	public TbEquipmentConfiglist queryEquipmentConfigByEqId(Integer eid) {
		String hql = " from TbEquipmentConfiglist where eid="+eid+"";
		log.info("hql="+hql);
		List<TbEquipmentConfiglist> list = (List<TbEquipmentConfiglist>)findAll(hql);
		if( !list.isEmpty() && list.size()>0 ){
			log.info("设备配置列表id="+list.get(0).getId());
			return list.get(0);
		}
		return null;
	}

}
